package com.org.netty.rpc.nettyStarter;

import com.org.netty.rpc.entity.School;
import com.org.netty.rpc.entity.Student;
import com.org.netty.rpc.provider.RpcServiceImpl;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

/**
 * @author: Lixy
 * @email: 1908598362@qq.com
 * @date: 2022/2/11 星期五 19:37
 */
public class nettyServerHandler extends ChannelInboundHandlerAdapter {


    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("message:"+msg);

        //客户端在调用服务器api时，必须要定义一个协议
        //比如每次发消息都要带xxx字符串，为一个定式 规范是应该有一个RPCProtocol  即完整的协议
        if (msg.toString().startsWith("###*")){

            School school = new RpcServiceImpl().chooseShl();
            String result = new RpcServiceImpl().SendHello(msg.toString().substring(msg.toString().lastIndexOf("*") + 1));

            ctx.writeAndFlush(school);
        }
    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("出异常...:"+cause.getStackTrace());
        ctx.close();
    }
}
